package com.practice.ajCollections010.list.arraylist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * ArrayList 遍历
 */
public class Traversal {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            list.add(Integer.toString(i));
        }

        /**
         * 遍历方法一：传统下标迭代遍历
         * 优点：可以取到下标
         * 缺点：代码比较繁琐
         */
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println("====================================");

        /**
         * 遍历方法二：foreach 遍历
         * 优点：代码相比于遍历方法一更加简洁
         * 缺点：无法获取当前元素的下标
         */
        for (String item : list) {
            System.out.println(item);
        }

        System.out.println("====================================");

        /**
         * 遍历方法三：iterator 迭代器迭代
         *
         */
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        System.out.println("====================================");

        /**
         * 遍历方法四：for 遍历 iterator 迭代器
         */
        for (Iterator iteratorIterm = iterator; iterator.hasNext(); ) {
            System.out.println(iterator.next());
        }

        System.out.println("====================================");

        /**
         * 遍历方式五：遍历 listIterator 迭代器
         */
        ListIterator<String> stringListIterator = list.listIterator();
        while (stringListIterator.hasNext()) {
            System.out.println(stringListIterator.next());
        }
        while (stringListIterator.hasPrevious()) {
            System.out.println(stringListIterator.previous());
        }
        for (ListIterator iteratorItem = stringListIterator; iteratorItem.hasPrevious(); ) {
            System.out.println(iteratorItem.previous());
        }

        System.out.println("====================================");

        /**
         * 遍历方法六
         */
        list.stream().forEach(iterm -> {
            System.out.println(iterm);
        });

    }
}
